<?php

namespace App\Http\Controllers\Admin;

use App\Common\CommonHelpers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Models\Menu;
use App\Traits\ModelAttributes;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Validator;


class MenuController extends Controller
{
    use ModelAttributes;
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin.menu.index');
    }

    /**
     * 菜单列表
     * @param Request $request
     * @return array
     */
    public function apiList(Request $request) {
        $admin = Auth()->guard('admin')->user();

        $list = Menu::buildPermissionMenus($admin->id, $admin->is_super);

        return CommonHelpers::responseFormat(0, $list);
    }

    public function apiTableList(Request $request) {
        $menus = Menu::select('id','name','pid', 'is_menu', 'route', 'icon', 'sort')
            ->orderBy('pid', 'asc')
            ->orderBy('sort', 'desc')
            ->get()->toArray();

        return CommonHelpers::responseFormat(0, $menus);
    }

    /**
     * 创建菜单
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create() {
        $selectOptions = Menu::selectOptions();
        return view('admin.menu.create', ['selectOptions' => $selectOptions]);
    }

    /**
     * 保存菜单
     * @param Request $request
     * @return array
     */
    public function store(Request $request) {
        $validateRules = [
            'name' => "required|max:255"
        ];
        $validateMessages = [
            'name.required' => '请输入菜单标题'
        ];

        $validator = Validator::make($request->all(), $validateRules, $validateMessages);
        if ($validator->fails()) {
            $message = $validator->errors()->first();
            return CommonHelpers::responseFormat(1000, null, $message);
        }

        $admin = Auth()->guard('admin')->user();
        $name = $request->get('name', false);
        $url = $request->get('route', '');

        $icon = "";
        if($request->get('icon')) $icon = $request->get('icon');

        $data = [
            'name'              => $name,
            'pid'               => $request->get('pid', 0),
            'route'             => $url,
            'icon'              => $icon,
            'is_menu'           => $request->get('is_menu', 0),
            'is_disabled'       => $request->get('is_disabled', 0),
            'sort'              => $request->get('sort', 50)
        ];

        \DB::beginTransaction();
        try {
            $model = Menu::create($data);
        }
        catch (\Exception $exception) {
            \DB::rollBack();
            return CommonHelpers::responseFormat(1000, null, $exception->getMessage());
        }
        \DB::commit();
        return CommonHelpers::responseFormat(0);
    }

    /**
     * 编辑菜单
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id) {
        $model = Menu::find($id);
        $selectOptions = Menu::selectOptions();
        return view('admin.menu.form', ['model' => $model, 'selectOptions' => $selectOptions]);
    }

    /**
     * 更新菜单内容
     * @param Request $request
     * @param $id
     * @return array
     */
    public function update(Request $request, $id) {
        $model = Menu::find($id);
        if($model) {
            $url = $request->get('route', '');
            $data = [
                'name'              => $request->get('name'),
                'pid'               => $request->get('pid', 0),
                'route'             => $url,
                'icon'              => $request->get('icon'),
                'is_disabled'       => $request->get('is_disabled'),
                'is_menu'           => $request->get('is_menu'),
                'sort'              => $request->get('sort', 50)
            ];
            \DB::beginTransaction();
            try {
                if ($model->update($data)) {
                    \DB::commit();
                    return CommonHelpers::responseFormat(0);
                }
                else {
                    \DB::rollBack();
                    return CommonHelpers::responseFormat(1000, null, "");
                }
            }
            catch (\Exception $exception) {
                \DB::rollBack();
                return CommonHelpers::responseFormat(1000, null, $exception->getMessage());
            }
        }
    }

    /**
     * 删除菜单
     * @param Request $request
     * @return array
     */
    public function delete(Request $request) {
        $id = $request->get('id');
        $model = Menu::find($id);
        if($model) {
            if(!$child = Menu::where(['pid' => $id])->first()) {
                if ($model->delete()) {
                    return CommonHelpers::responseFormat(0);
                }
            }
        }
        return CommonHelpers::responseFormat(1000, null, '请先删除下级菜单');
    }
}
